{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2c9e24c4-e402-4b7b-82c1-4590a9960570",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_experimental.agents.agent_toolkits import create_csv_agent\n",
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aca6b083-76ed-4567-b044-0b92c8fce8d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = create_csv_agent(\n",
    "    llm=ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0,),\n",
    "    path=\"house_price.csv\",\n",
    "    verbose=True,\n",
    "    agent_executor_kwargs={\"handle_parsing_errors\": True}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d5a1de8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AgentExecutor(verbose=True, agent=RunnableAgent(runnable=RunnableAssign(mapper={\n",
       "  agent_scratchpad: RunnableLambda(lambda x: format_log_to_str(x['intermediate_steps']))\n",
       "})\n",
       "| PromptTemplate(input_variables=['agent_scratchpad', 'input'], partial_variables={'df_head': '|    |    price |   area |   bedrooms |   bathrooms |   stories | mainroad   | guestroom   | basement   | hotwaterheating   | airconditioning   |   parking | prefarea   | furnishingstatus   |\\n|---:|---------:|-------:|-----------:|------------:|----------:|:-----------|:------------|:-----------|:------------------|:------------------|----------:|:-----------|:-------------------|\\n|  0 | 13300000 |   7420 |          4 |           2 |         3 | yes        | no          | no         | no                | yes               |         2 | yes        | furnished          |\\n|  1 | 12250000 |   8960 |          4 |           4 |         4 | yes        | no          | no         | no                | yes               |         3 | no         | furnished          |\\n|  2 | 12250000 |   9960 |          3 |           2 |         2 | yes        | no          | yes        | no                | no                |         2 | yes        | semi-furnished     |\\n|  3 | 12215000 |   7500 |          4 |           2 |         2 | yes        | no          | yes        | no                | yes               |         3 | yes        | furnished          |\\n|  4 | 11410000 |   7420 |          4 |           1 |         2 | yes        | yes         | yes        | no                | yes               |         2 | no         | furnished          |', 'tools': 'python_repl_ast: A Python shell. Use this to execute python commands. Input should be a valid python command. When using this tool, sometimes output is abbreviated - make sure it does not look abbreviated before using it in your answer.', 'tool_names': 'python_repl_ast'}, template='\\nYou are working with a pandas dataframe in Python. The name of the dataframe is `df`.\\nYou should use the tools below to answer the question posed of you:\\n\\n{tools}\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [{tool_names}]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can repeat N times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\n\\nThis is the result of `print(df.head())`:\\n{df_head}\\n\\nBegin!\\nQuestion: {input}\\n{agent_scratchpad}')\n",
       "| RunnableBinding(bound=ChatOpenAI(client=<openai.resources.chat.completions.Completions object at 0x11c8fa510>, async_client=<openai.resources.chat.completions.AsyncCompletions object at 0x11c901c10>, temperature=0.0, openai_api_key=SecretStr('**********'), openai_proxy=''), kwargs={'stop': ['\\nObservation']})\n",
       "| ReActSingleInputOutputParser(), input_keys_arg=['input'], return_keys_arg=['output'], stream_runnable=True), tools=[PythonAstREPLTool(locals={'df':         price  area  bedrooms  bathrooms  stories mainroad guestroom basement  \\\n",
       "0    13300000  7420         4          2        3      yes        no       no   \n",
       "1    12250000  8960         4          4        4      yes        no       no   \n",
       "2    12250000  9960         3          2        2      yes        no      yes   \n",
       "3    12215000  7500         4          2        2      yes        no      yes   \n",
       "4    11410000  7420         4          1        2      yes       yes      yes   \n",
       "..        ...   ...       ...        ...      ...      ...       ...      ...   \n",
       "540   1820000  3000         2          1        1      yes        no      yes   \n",
       "541   1767150  2400         3          1        1       no        no       no   \n",
       "542   1750000  3620         2          1        1      yes        no       no   \n",
       "543   1750000  2910         3          1        1       no        no       no   \n",
       "544   1750000  3850         3          1        2      yes        no       no   \n",
       "\n",
       "    hotwaterheating airconditioning  parking prefarea furnishingstatus  \n",
       "0                no             yes        2      yes        furnished  \n",
       "1                no             yes        3       no        furnished  \n",
       "2                no              no        2      yes   semi-furnished  \n",
       "3                no             yes        3      yes        furnished  \n",
       "4                no             yes        2       no        furnished  \n",
       "..              ...             ...      ...      ...              ...  \n",
       "540              no              no        2       no      unfurnished  \n",
       "541              no              no        0       no   semi-furnished  \n",
       "542              no              no        0       no      unfurnished  \n",
       "543              no              no        0       no        furnished  \n",
       "544              no              no        0       no      unfurnished  \n",
       "\n",
       "[545 rows x 13 columns]})], handle_parsing_errors=True)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4ab28d3b-a5d0-44d6-9cf3-18699d1ec4c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: 我们可以使用`shape`属性来获取数据集的行数\n",
      "Action: python_repl_ast\n",
      "Action Input: df.shape[0]\u001b[0m\u001b[36;1m\u001b[1;3m545\u001b[0m\u001b[32;1m\u001b[1;3m数据集有545行\n",
      "Final Answer: 545行\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '数据集有多少行？用中文回复', 'output': '545行'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke({\"input\": \"数据集有多少行？用中文回复\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b4ac113d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: We need to list out the variables present in the dataset.\n",
      "Action: Use python_repl_ast to get the column names of the dataframe.\n",
      "Action Input: df.columns\u001b[0mUse python_repl_ast to get the column names of the dataframe. is not a valid tool, try one of [python_repl_ast].\u001b[32;1m\u001b[1;3mI need to use the correct tool to get the column names of the dataframe.\n",
      "Action: python_repl_ast\n",
      "Action Input: df.columns\u001b[0m\u001b[36;1m\u001b[1;3mIndex(['price', 'area', 'bedrooms', 'bathrooms', 'stories', 'mainroad',\n",
      "       'guestroom', 'basement', 'hotwaterheating', 'airconditioning',\n",
      "       'parking', 'prefarea', 'furnishingstatus'],\n",
      "      dtype='object')\u001b[0m\u001b[32;1m\u001b[1;3m数据集包含的变量有：price, area, bedrooms, bathrooms, stories, mainroad, guestroom, basement, hotwaterheating, airconditioning, parking, prefarea, furnishingstatus\n",
      "\n",
      "Final Answer: 数据集包含的变量有：price, area, bedrooms, bathrooms, stories, mainroad, guestroom, basement, hotwaterheating, airconditioning, parking, prefarea, furnishingstatus\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '数据集包含哪些变量？用中文回复',\n",
       " 'output': '数据集包含的变量有：price, area, bedrooms, bathrooms, stories, mainroad, guestroom, basement, hotwaterheating, airconditioning, parking, prefarea, furnishingstatus'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke({\"input\": \"数据集包含哪些变量？用中文回复\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cb8ed1bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: We need to calculate the average price of all houses in the dataset.\n",
      "Action: Use python_repl_ast to calculate the average price.\n",
      "Action Input: df['price'].mean()\u001b[0mUse python_repl_ast to calculate the average price. is not a valid tool, try one of [python_repl_ast].\u001b[32;1m\u001b[1;3mI will use the correct tool to calculate the average price.\n",
      "Action: python_repl_ast\n",
      "Action Input: df['price'].mean()\u001b[0m\u001b[36;1m\u001b[1;3m4766729.247706422\u001b[0m\u001b[32;1m\u001b[1;3m所有房子的价格平均值是4766729.25\n",
      "Final Answer: 所有房子的价格平均值是4766729.25\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '数据集里，所有房子的价格平均值是多少？用中文回复', 'output': '所有房子的价格平均值是4766729.25'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke({\"input\": \"数据集里，所有房子的价格平均值是多少？用中文回复\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8709aa44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: We need to find out the unique values in the 'furnishingstatus' column and interpret what they mean in Chinese.\n",
      "Action: python_repl_ast\n",
      "Action Input: df['furnishingstatus'].unique()\u001b[0m\u001b[36;1m\u001b[1;3m['furnished' 'semi-furnished' 'unfurnished']\u001b[0m\u001b[32;1m\u001b[1;3m我现在知道最终答案\n",
      "Final Answer: 装修状态包括'furnished'（已装修）、'semi-furnished'（半装修）和'unfurnished'（未装修）。它们分别表示房子的装修程度。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': '数据集里，所有房子的装修状态包含哪些种类？你认为它们具体表示什么意思？用中文回复',\n",
       " 'output': \"装修状态包括'furnished'（已装修）、'semi-furnished'（半装修）和'unfurnished'（未装修）。它们分别表示房子的装修程度。\"}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.invoke({\n",
    "    \"input\": \"数据集里，所有房子的装修状态包含哪些种类？你认为它们具体表示什么意思？用中文回复\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5b11166",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
